package com.algorithm.base.greedy;

import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.Comparator;

/**
 * 435. 无重叠区间
 *
 * @author Amadeus
 * @date 2023/11/24
 */
@Service
public class EraseOverlapIntervals {

    public int eraseOverlapIntervals(int[][] intervals) {
        Arrays.sort(intervals, Comparator.comparingInt(a -> a[1]));

        int arrows = 0;
        int end = intervals[0][1];

        for (int i = 1; i < intervals.length; i++) {
            if (end > intervals[i][0]) {
                arrows++;
                end = Math.min(end, intervals[i][1]);
            } else {
                end = intervals[i][1];
            }
        }
        return arrows;
    }

}
